package com.selfdiscipline.ui.components.charts

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * 条形图表组件
 *
 * @param data 图表数据列表
 * @param barColor 条形图颜色
 * @param modifier 修饰符
 */
@Composable
fun BarChart(
    data: List<ChartData>,
    barColor: Color = Color(0xFF3F51B5),
    modifier: Modifier = Modifier
) {
    if (data.isEmpty()) {
        return
    }
    
    val maxValue = data.maxOfOrNull { it.value } ?: 0f
    val axisColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f)
    
    Column(modifier = modifier) {
        Canvas(
            modifier = Modifier
                .weight(1f)
                .fillMaxWidth()
        ) {
            val barWidth = size.width / data.size * 0.6f
            val spacing = size.width / data.size * 0.4f
            val bottomPadding = 30f
            
            // 绘制Y轴
            drawLine(
                color = axisColor,
                start = Offset(0f, 0f),
                end = Offset(0f, size.height - bottomPadding),
                strokeWidth = 1f
            )
            
            // 绘制X轴
            drawLine(
                color = axisColor,
                start = Offset(0f, size.height - bottomPadding),
                end = Offset(size.width, size.height - bottomPadding),
                strokeWidth = 1f
            )
            
            // 绘制柱状图
            data.forEachIndexed { index, chartData ->
                val barHeight = if (maxValue > 0) 
                    (chartData.value / maxValue) * (size.height - bottomPadding - 20f) 
                else 0f
                
                val left = index * (barWidth + spacing) + spacing / 2
                val top = size.height - bottomPadding - barHeight
                
                // 绘制柱子
                drawRect(
                    color = chartData.color,
                    topLeft = Offset(left, top),
                    size = Size(barWidth, barHeight)
                )
            }
        }
        
        // 绘制X轴标签
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 4.dp),
            horizontalArrangement = Arrangement.SpaceAround
        ) {
            data.forEach { chartData ->
                Text(
                    text = chartData.label,
                    style = MaterialTheme.typography.bodySmall,
                    textAlign = TextAlign.Center,
                    modifier = Modifier.weight(1f)
                )
            }
        }
    }
}

/**
 * 水平条形图表组件
 *
 * @param data 图表数据列表
 * @param barColor 条形图颜色
 * @param modifier 修饰符
 */
@Composable
fun HorizontalBarChart(
    data: List<ChartData>,
    barColor: Color = Color(0xFF3F51B5),
    modifier: Modifier = Modifier
) {
    if (data.isEmpty()) {
        return
    }
    
    val maxValue = data.maxOfOrNull { it.value } ?: 0f
    val topItems = data.sortedByDescending { it.value }.take(8)
    val surfaceVariantColor = MaterialTheme.colorScheme.surfaceVariant
    val textColor = MaterialTheme.colorScheme.onSurface
    val density = LocalDensity.current
    val paint = remember {
        android.graphics.Paint().apply {
            color = android.graphics.Color.BLACK
            textSize = with(density) { 12.sp.toPx() }
            textAlign = android.graphics.Paint.Align.LEFT
        }
    }
    
    Column(modifier = modifier) {
        topItems.forEach { chartData ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 4.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = chartData.label,
                    style = MaterialTheme.typography.bodySmall,
                    modifier = Modifier.weight(0.3f)
                )
                
                Column(
                    modifier = Modifier.weight(0.7f)
                ) {
                    Canvas(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(24.dp)
                    ) {
                        val barWidth = if (maxValue > 0) 
                            (chartData.value / maxValue) * size.width 
                        else 0f
                        
                        // 绘制背景
                        drawRect(
                            color = surfaceVariantColor,
                            size = Size(size.width, size.height)
                        )
                        
                        // 绘制进度条
                        drawRect(
                            color = chartData.color,
                            size = Size(barWidth, size.height)
                        )
                        
                        // 绘制数值文本
                        val valueText = "${chartData.value.toInt()}"
                        drawIntoCanvas { canvas ->
                            canvas.nativeCanvas.drawText(
                                valueText,
                                barWidth + 4f,
                                size.height / 2 + 4f,
                                paint
                            )
                        }
                    }
                }
            }
        }
    }
} 